package com.jkoss.wine.customer.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jkoss.base.controller.BaseController;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.vo.DwzPageBean;
import com.jkoss.wine.customer.entity.Shop;
import com.jkoss.wine.customer.entity.ShopFollow;
import com.jkoss.wine.customer.service.IShopFollowService;

@Controller
@RequestMapping("/customer/shopFollow")
public class ShopFollowController extends BaseController {
	@Autowired
	private IShopFollowService iShopFollowService;

	@RequestMapping("/check")
	@RequiresPermissions("/customer/shopFollow/check")
	public String check(String id, String maid, ModelMap map, DwzPageBean dwzPageBean, HttpServletRequest request,
			HttpServletResponse response) {
		dwzPageBean.getCountResultMap().put("maid", maid);
		Wrapper wrapper = Condition.create().eq("spid", id).like("maid", maid).ne("del", 1);
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		} else {
			wrapper.orderBy("add_time", false);
		}
		Page<Shop> resultPage = iShopFollowService.selectPage(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		map.put("spid", id);
		return "customer/follow/list";
	}

	@RequestMapping("/toInsert")
	@RequiresPermissions("/customer/shopFollow/toInsert")
	public String toInsert(String spid, ModelMap map) {
		map.put("spid", spid);
		return "customer/follow/edit";

	}

	@RequestMapping("/insert")
	@RequiresPermissions("/customer/shopFollow/toInsert")
	@ResponseBody
	public Object insert(ShopFollow shopFollow, String spid) {
		shopFollow.setAddTime(CommonUtil.getDate());
		shopFollow.setDel(0);
		shopFollow.setSpid(spid);
		shopFollow.setMaid(getUserId());
		shopFollow.setOperator(getUserId());
		if (iShopFollowService.insert(shopFollow)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}

	}

	@RequestMapping("/toUpdate")
	@RequiresPermissions("/customer/shopFollow/toUpdate")
	public String toUpdate(String id, ModelMap map) {
		ShopFollow shopFollow = iShopFollowService.selectById(id);
		map.put("record", shopFollow);
		return "customer/follow/edit";

	}

	@RequestMapping("/update")
	@RequiresPermissions("/customer/shopFollow/toUpdate")
	@ResponseBody
	public Object update(ShopFollow shopFollow) {
		if (iShopFollowService.updateById(shopFollow)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}

	}

	@RequestMapping("/delete")
	@RequiresPermissions("/customer/shopFollow/delete")
	@ResponseBody
	public Object delete(String[] id) {
		List list = new ArrayList();
		for (String str : id) {
			ShopFollow shopFollow = iShopFollowService.selectById(str);
			shopFollow.setDel(1);
			list.add(shopFollow);
		}
		if (iShopFollowService.updateBatchById(list)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}
}
